<?php

//管理员模型
class Managers_model extends Model {

	function Managers_model()
	{
		parent::Model();	
		$this->load->database();
		$this->load->library("session");
	}
	
	//添加
	function add($regdata = NULL)
	{
		if(is_null($regdata)) return false;
		//验证 ...
		//execute sql
		$data = array(
			"email" => $regdata["email"],
			"username" => $regdata["username"],
			"password" => md5($regdata["password"]),
			"description" => $regdata["description"],
			"regtime" => time(),
		);
		$result = $this->db->insert("managers", $data);
		return $result;
	}
	
	//编辑
	function edit($regdata = NULL, $userid = 0)
	{
		if(is_null($regdata) || $userid == 0) return false;
		//验证 ...
		//execute sql
		$data = array(
			"email" => $regdata["email"],
			"username" => $regdata["username"],
			"password" => md5($regdata["password"]),
			"description" => $regdata["description"],
			"regtime" => time(),
		);
		if (trim($regdata["password"]) == "") {
			unset($data["password"]);
		}
		$this->db->where("id", $userid);
		$result = $this->db->update("managers", $data);
		return $result;
	}
	
	//登陆
	function login($logindata = NULL)
	{
		if(is_null($logindata)) return false;
		//验证 ...
		$query = $this->db->get_where("managers", array("username"=>$logindata["username"], "password"=>md5($logindata["password"])));
		if($query->num_rows()>0) {//登陆成功 -> session
			$account = $query->row_array();
			$sess_data = array(
				"admin_id" => $account["id"],
				"admin_username" => $account["username"],
			);
			$this->session->set_userdata($sess_data);
			//更新最后登陆时间
			$this->update_lastlogin($account["id"]);
			return $sess_data;
		} 
		return false;
	}
	
	//获取管理员列表
	function listitem($offset = 0, $pagecount = 20, $order = "id", $desc = "desc")
	{
		$items = array();
		$query = $this->db->from("managers")->limit($pagecount, $offset)->order_by($order, $desc)->get();
		if ($query->num_rows()>0) {
			$items = $query->result_array();
		}
		return $items;
	}
	
	function countitems()
	{
		return $this->db->count_all_results("managers");
	}
	
	//更新最后登陆时间
	function update_lastlogin($id)
	{
		$this->db->where("id", $id);
		$result = $this->db->update("managers", array("lastlogin"=>time()));
		return $result;
	}

	//退出登陆
	function logout()
	{
		$sess_data = array(
			"admin_id" => "",
			"admin_username" => "",
		);
		$this->session->unset_userdata($sess_data);
	}
	
	//检测登陆状态
	function is_logined()
	{
		$admin_id = $this->session->userdata("admin_id");
		if($admin_id) return $admin_id;
		return 0;
	}
	
	//获取登陆信息
	function get_account()
	{
		if(!$this->is_logined()) return false;
		$sess_data = array(
			"admin_id" => $this->session->userdata("admin_id"),
			"admin_username" => $this->session->userdata("admin_username"),
		);
		return $sess_data;
	}
	
	//获取帐号信息
	function get($userid)
	{
		$query = $this->db->get_where("managers", array("id"=>$userid));
		if($query->num_rows()>0) {
			return $query->row_array();
		}
		return false;
	}
	
	//删除
	function delete($userid = 0)
	{
		if($userid == 0) return false;
		$this->db->delete("managers", array('id'=>$userid));
	}
	
	//检测用户名是否存在
	function exsits_username($username, $userid = 0)
	{
		$query = $this->db->get_where("managers", array("username"=>$username, "id !="=>$userid));
		if($query->num_rows()>0) {
			return true;
		} else {
			return false;
		}
	}
	
}

/* End of file */